﻿using CQIE.LabAssist.DataAccesss;
using CQIE.LabAssist.Models;
using Microsoft.AspNetCore.Mvc;

namespace CQIE.LabAssist.Controllers
{
    /// <summary>
    /// 实验室控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LaboratoryController : Controller
    {
        private readonly ILaboratoryService _laboratoryService;

        public LaboratoryController(_DbContext _DbContext, ILaboratoryService laboratoryService)
        {
            _laboratoryService = laboratoryService;
        }

        #region 获取该楼层的所有实验室
        /// <summary>
        /// 获取该楼层的所有实验室
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getAllLaboratory(int floorid)
        {
            return new JsonResult(_laboratoryService.getAllLaboratory(floorid));
        }
        #endregion

        /// <summary>
        /// 根据用户id获取所有教室
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getLaboratoryByUserId(int userId)
        {
            return new JsonResult(_laboratoryService.getLaboratoryByUserId(userId));
        }

        #region 新增实验室

        public record Lab
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Number { get; set; }
            public int floorId { get; set; }    
            public int UserId { get; set; }
        }
        /// <summary>
        /// 新增实验室
        /// </summary>
        /// <param name="lab"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult addLaboratory([FromBody] Lab lab)
        {
            Laboratory laboratory = new Laboratory();
            laboratory.Name = lab.Name;
            laboratory.Nubmer = lab.Number;
            laboratory.UserId = lab.UserId;
            laboratory.floorId = lab.floorId;
            if (_laboratoryService.addLaboratory(laboratory))
            {
                return new JsonResult("添加成功");
            }
            else
            {
                return new JsonResult("添加失败");

            }

        }
        #endregion

        #region 删除实验室

        /// <summary>
        /// 删除实验室
        /// </summary>
        /// <param name="building"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult deleteLaboratory(int labId)
        {
            if (_laboratoryService.deleteLaboratory(labId))
            {
                return new JsonResult("删除成功");
            }
            else
            {
                return new JsonResult("删除失败");

            }

        }
        #endregion

        #region 修改楼栋
        /// <summary>
        /// 修改楼栋
        /// </summary>
        /// <param name="lab"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult updateLaboratory([FromBody] Lab lab )
        {
            Laboratory la = new Laboratory();
            la.Id = lab.Id;
            la.Name = lab.Name;
            la.Nubmer = lab.Number;
            la.floorId = lab.floorId;
            la.UserId = lab.UserId;


            if (_laboratoryService.updateLaboratory(la))
            {
                return new JsonResult("修改成功");
            }
            else
            {
                return new JsonResult("修改失败");
            }
        }
        #endregion
    }
}
